<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发送方" prop="sendSysCode">
        <el-select v-model="queryParams.sendSysCode" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.sys_code"
            :key="dict.value"
            :label="dict.label+'-'+dict.value"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="接收方" prop="recvSysCode">
        <el-select v-model="queryParams.recvSysCode" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.sys_code"
            :key="dict.value"
            :label="dict.label+'-'+dict.value"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文件名" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:sendHistory:add']"
        >文件发送</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddBach"
          v-hasPermi="['system:sendHistory:add']"
        >批量发送</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:sendHistory:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:sendHistory:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:sendHistory:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sendHistoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
<!--      <el-table-column label="备注" align="center" prop="remark">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.remark" :value="scope.row.remark"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="文件名称" align="center" prop="fileName"/>
      <el-table-column label="发送方系统号" align="center" prop="sendSysCode"/>
      <el-table-column label="接收方系统号" align="center" prop="recvSysCode"/>
<!--      <el-table-column label="发送状态" align="center" prop="flag"/>-->
      <el-table-column label="发送状态" align="center" prop="msg"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
            v-hasPermi="['monitor:operlog:query']"
          >详细</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:sendHistory:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sendHistory:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文件发送记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="接收方下系统号" prop="recvSysCode">-->
<!--          <el-input v-model="form.recvSysCode" placeholder="请输入接收方下系统号" />-->
<!--        </el-form-item>-->

        <el-form-item label="发送方" prop="sendSysCode">
          <el-select v-model="form.sendSysCode" placeholder="请选择系统号">
            <el-option
              v-for="dict in dict.type.sys_code"
              :key="dict.value"
              :label="dict.label+'-'+dict.value"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="接收方" prop="recvSysCode">
          <el-select v-model="form.recvSysCode" placeholder="请选择系统号">
            <el-option
              v-for="dict in dict.type.sys_code"
              :key="dict.value"
              :label="dict.label+'-'+dict.value"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="文件名" prop="fileIds">
          <el-select v-model="form.fileIds"  multiple placeholder="请选择" @change="onFileSelect"  @click.native="getFileList">
            <el-option
              v-for="item in fileList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

<!--        <el-form-item label="文件路径" prop="inputPath">-->
<!--          <el-input v-model="form.inputPath" placeholder="请输入文件路径" />-->
<!--        </el-form-item>-->

      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改文件发送记录对话框 -->
    <el-dialog :title="title" :visible.sync="openBach" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--        <el-form-item label="接收方下系统号" prop="recvSysCode">-->
        <!--          <el-input v-model="form.recvSysCode" placeholder="请输入接收方下系统号" />-->
        <!--        </el-form-item>-->

        <el-form-item label="发送方" prop="sendSysCode">
          <el-select v-model="form.sendSysCode" placeholder="请选择系统号">
            <el-option
              v-for="dict in dict.type.sys_code"
              :key="dict.value"
              :label="dict.label+'-'+dict.value"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="接收方" prop="recvSysCode">
          <el-select v-model="form.recvSysCode" placeholder="请选择系统号">
            <el-option
              v-for="dict in dict.type.sys_code"
              :key="dict.value"
              :label="dict.label+'-'+dict.value"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="文件路径" prop="inputPath">
          <el-input v-model="form.inputPath" placeholder="请输入文件路径" />
        </el-form-item>

      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 操作日志详细 -->
    <el-dialog title="操作日志详细" :visible.sync="openLog" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
<!--          <el-col :span="12">-->
<!--            <el-form-item-->
<!--              label="登录信息："-->
<!--            >{{ form.operName }} / {{ form.operIp }} / {{ form.operLocation }}</el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="24">-->
<!--            <el-form-item label="请求参数：">{{ form.operParam }}</el-form-item>-->
<!--          </el-col>-->
          <el-col :span="24">
            <el-form-item label="返回参数：">{{ form.info }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作状态：">
              {{ form.msg }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作时间：">{{ parseTime(form.updateTime) }}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建时间：">{{ parseTime(form.createTime) }}</el-form-item>
          </el-col>
<!--          <el-col :span="24">-->
<!--            <el-form-item label="异常信息：" v-if="form.status === 1">{{ form.errorMsg }}</el-form-item>-->
<!--          </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openLog = false">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listSendHistory, getSendHistory, delSendHistory, addSendHistory, updateSendHistory } from "@/api/system/sendHistory"
import { allList } from "@/api/system/create"

const parseTime = function(time) {
  const values = (time || '').split(':');
  if (values.length >= 2) {
    const hours = parseInt(values[0], 10);
    const minutes = parseInt(values[1], 10);

    return {
      hours,
      minutes
    };
  }
  /* istanbul ignore next */
  return null;
};

export default {
  name: "SendHistory",
  dicts: ['sys_code'], // 只加载需要的字典
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件发送记录表格数据
      sendHistoryList: [],
      //文件列表
      fileList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openBach: false,
      openLog: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: null,
        sendSysCode: null,
        recvSysCode: null,
        flag: null,
        msg: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "菜单ID不能为空", trigger: "blur" }
        ],
        sendSysCode: [
          { required: true, message: "发送方不能为空", trigger: "blur" }
        ],
        recvSysCode: [
          { required: true, message: "接收方不能为空", trigger: "blur" }
        ],
        fileIds: [
          { required: false, message: "文件名不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
    // this.getFileList()
  },
  methods: {
    /** 查询文件发送记录列表 */
    getList() {
      this.loading = true
      listSendHistory(this.queryParams).then(response => {
        this.sendHistoryList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getFileList() {
      // 正确调用方式
      allList().then(response => {  // 注意这里加了 ()
        this.fileList = response.rows || []
      }).catch(error => {
        console.error('获取文件列表失败:', error)
      })
    },
    onFileSelect(fileId) {
      const selectedFile = this.fileList.find(item => item.id === fileId)
      this.form.fileName = selectedFile ? selectedFile.name : ''
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.openLog = true
      this.form = row
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openBach = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        fileName: null,
        sendSysCode: null,
        recvSysCode: null,
        fileId: null,
        inputPath: null,
        fileIds: [],
        flag: null,
        msg: null,
        info:null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加文件发送记录"
    },
    /** 新增按钮操作 */
    handleAddBach() {
      this.reset()
      this.openBach = true
      this.title = "添加文件发送记录"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSendHistory(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改文件发送记录"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSendHistory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.openBach = false
              this.getList()
            })
          } else {
            addSendHistory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.openBach = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除文件发送记录编号为"' + ids + '"的数据项？').then(function() {
        return delSendHistory(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sendHistory/export', {
        ...this.queryParams
      }, `sendHistory_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
